package util

import (
	"math"
	"nspel/entity"
	"nspel/nspel"
	"strconv"
	"time"
)

func GetStudentsListAdministrator(provinceId int64, cityId int64, areaId int64, schoolName string, joinYear int64, schoolRollId string, pageSize int64, currentPage int64) (count int64, students []entity.Student, err error) {
	student := new(entity.Student)
	ormEng := nspel.GetOrmEngine().QueryTable(student.TableName())
	hashKey := ""
	if provinceId != 0 || cityId != 0 || areaId != 0 || schoolName != "" || joinYear != 0 {
		_, schools, err := GetSchoolsListAdministrator(provinceId, cityId, areaId, schoolName, joinYear, math.MaxInt32, 1)
		if err != nil {
			return 0, nil, err
		}
		if len(schools) == 0 {
			return 0, []entity.Student{}, nil
		}
		schoolId := make([]int64, len(schools))
		for i := 0; i < len(schools); i++ {
			schoolId[i] = schools[i].Id
		}
		ormEng = ormEng.Filter("school_id__in", schoolId)
		if provinceId != 0 {
			hashKey += "provinceId[" + strconv.FormatInt(provinceId, 10) + "]."
		}
		if cityId != 0 {
			hashKey += "cityId[" + strconv.FormatInt(cityId, 10) + "]."
		}
		if areaId != 0 {
			hashKey += "areaId[" + strconv.FormatInt(areaId, 10) + "]."
		}
		hashKey += "schoolName[\"" + schoolName + "\"]."
		if joinYear != 0 {
			hashKey += "joinYear[" + strconv.FormatInt(joinYear, 10) + "]."
		}
	}
	ormEng = ormEng.Filter("school_roll_id__contains", schoolRollId)
	hashKey += "schoolRollId[" + schoolRollId + "]."
	count, err = student.CustomCount(&ormEng, hashKey+"count")
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	students, err = student.CustomSelectAll(&ormEng, hashKey)
	return
}

func GetStudentsListArea(areaId int64, joinYear int64, schoolId int64, schoolRollId string, pageSize int64, currentPage int64) (count int64, students []entity.Student, err error) {
	student := new(entity.Student)
	ormEng := nspel.GetOrmEngine().QueryTable(student.TableName()).Filter("experiment_area_id", areaId)
	hashKey := "experimentAreaId[" + strconv.FormatInt(areaId, 10) + "]."
	if joinYear != 0 || schoolId != 0 {
		_, schools, err := GetSchoolsListArea(areaId, joinYear, schoolId, math.MaxInt32, 1)
		if err != nil {
			return 0, nil, err
		}
		if len(schools) == 0 {
			return 0, []entity.Student{}, nil
		}
		schoolIds := make([]int64, len(schools))
		for i := 0; i < len(schools); i++ {
			schoolIds[i] = schools[i].Id
		}
		ormEng = ormEng.Filter("school_id__in", schoolIds)
		hashKey += "areaId[" + strconv.FormatInt(areaId, 10) + "]."
		if joinYear != 0 {
			hashKey += "joinYear[" + strconv.FormatInt(joinYear, 10) + "]."
		}
		if schoolId != 0 {
			hashKey += "cityId[" + strconv.FormatInt(schoolId, 10) + "]."
		}
	}
	ormEng = ormEng.Filter("school_roll_id__contains", schoolRollId)
	hashKey += "schoolRollId[" + schoolRollId + "]."
	count, err = student.CustomCount(&ormEng, hashKey+"count")
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	students, err = student.CustomSelectAll(&ormEng, hashKey)
	return
}

func GetStudentsListSchool(schoolId int64, schoolRollId string, pageSize int64, currentPage int64) (count int64, students []entity.Student, err error) {
	student := new(entity.Student)
	ormEng := nspel.GetOrmEngine().QueryTable(student.TableName()).Filter("school_id", schoolId).Filter("school_roll_id__contains", schoolRollId)
	hashKey := "schoolId[" + strconv.FormatInt(schoolId, 10) + "].schoolRollId[" + schoolRollId + "]"
	count, err = student.CustomCount(&ormEng, hashKey+"count")
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	students, err = student.CustomSelectAll(&ormEng, hashKey)
	return
}

func GetSex(sex byte) string {
	if sex == 1 {
		return "男"
	} else if sex == 2 {
		return "女"
	} else {
		return "未知"
	}
}

func GetGrade(grade string, class string) string {
	if len(class) < 4 {
		return "未知"
	}
	if grade == "11" {
		return class[0:4] + "级 " + "一年级"
	} else if grade == "12" {
		return class[0:4] + "级 " + "二年级"
	} else if grade == "13" {
		return class[0:4] + "级 " + "三年级"
	} else if grade == "14" {
		return class[0:4] + "级 " + "四年级"
	} else if grade == "15" {
		return class[0:4] + "级 " + "五年级"
	} else if grade == "16" {
		return class[0:4] + "级 " + "六年级"
	} else if grade == "21" {
		return class[0:4] + "级 " + "初一"
	} else if grade == "22" {
		return class[0:4] + "级 " + "初二"
	} else if grade == "23" {
		return class[0:4] + "级 " + "初三"
	} else if grade == "31" {
		return class[0:4] + "级 " + "高一"
	} else if grade == "32" {
		return class[0:4] + "级 " + "高二"
	} else if grade == "33" {
		return class[0:4] + "级 " + "高三"
	} else {
		return "未知"
	}
}

func GetGrades(grade string) string {
	if grade == "11" {
		return "一年级"
	} else if grade == "12" {
		return "二年级"
	} else if grade == "13" {
		return "三年级"
	} else if grade == "14" {
		return "四年级"
	} else if grade == "15" {
		return "五年级"
	} else if grade == "16" {
		return "六年级"
	} else if grade == "21" {
		return "初一"
	} else if grade == "22" {
		return "初二"
	} else if grade == "23" {
		return "初三"
	} else if grade == "31" {
		return "高一"
	} else if grade == "32" {
		return "高二"
	} else if grade == "33" {
		return "高三"
	} else {
		return "未知"
	}
}

func GetClass(class string) string {
	if len(class) < 7 {
		return "未知"
	}
	if class[5:6] == "0" {
		return class[6:7]
	} else {
		return class[5:7]
	}
}

func GetAge(birthday time.Time) string {
	if birthday.IsZero() {
		return ""
	}
	sub := time.Now().Year() - birthday.Year() + 1
	return strconv.Itoa(sub)
}

var nationMap map[string]string

func init() {
	nationMap = map[string]string{"1": "汉族", "2": "蒙古族", "3": "回族", "4": "藏族", "5": "维吾尔族", "6": "苗族", "7": "彝族", "8": "壮族", "9": "布依族", "10": "朝鲜族",
		"11": "满族", "12": "侗族", "13": "瑶族", "14": "白族", "15": "土家族", "16": "哈尼族", "17": "哈萨克族", "18": "傣族", "19": "黎族", "20": "傈僳族",
		"21": "佤族", "22": "畲族", "23": "高山族", "24": "拉祜族", "25": "水族", "26": "东乡族", "27": "纳西族", "28": "景颇族", "29": "柯尔克孜族", "30": "土族",
		"31": "达斡尔族", "32": "仫佬族", "33": "羌族", "34": "布朗族", "35": "撒拉族", "36": "毛难族", "37": "仡佬族", "38": "锡伯族", "39": "阿昌族", "40": "普米族",
		"41": "塔吉克族", "42": "怒族", "43": "乌孜别克族", "44": "俄罗斯族", "45": "鄂温克族", "46": "崩龙族", "47": "保安族", "48": "裕固族", "49": "京族", "50": "塔塔尔族",
		"51": "独龙族", "52": "鄂伦春族", "53": "赫哲族", "54": "门巴族", "55": "珞巴族", "56": "基诺族", "97": "其他", "98": "外国血统"}
}

func GetNation(nation string) string {
	nationStr, ok := nationMap[nation]
	if !ok {
		return "未知"
	}
	return nationStr
}
